package business.employee.managers;

import employee.entities.EmployeeVO;
import employee.exceptions.IncompleteEmployeeException;
import persistence.employee.employeeDAO.EmployeeDAO;
import business.employee.entities.Employee;
import business.employee.interfaces.EmployeeMgt;

public class EmployeeMgr implements EmployeeMgt {
	
	private static EmployeeMgr instance = null;

	private EmployeeMgr() {
	}

	public static EmployeeMgr getInstance() {
		if (instance == null)
			instance = new EmployeeMgr();
		return instance;
	}

	@Override
	public EmployeeVO[] getEmployees() {
		return this.toVOs(EmployeeDAO.getInstance().getEmployees());
	}

	@Override
	public EmployeeVO getEmployee(int id) {
		Employee aux = EmployeeDAO.getInstance().getEmployee(id);
		return (aux!=null) ? aux.toVO() : null;
	}

	@Override
	public void update(EmployeeVO emp) throws IncompleteEmployeeException {
		if(emp!=null && emp.getPassword()!=null){
			EmployeeDAO.getInstance().update(new Employee(emp));
		}else{
			throw new IncompleteEmployeeException("Faltan datos al empleado");
		}

	}

	@Override
	public void delete(int id) {
		EmployeeDAO.getInstance().delete(id);
	}

	@Override
	public void add(EmployeeVO emp) throws IncompleteEmployeeException {
		if(emp!=null && emp.getPassword()!=null && emp.getLastName()!=null){
			EmployeeDAO.getInstance().add(new Employee(emp));
		}else{
			throw new IncompleteEmployeeException("Faltan datos al empleado");
		}

	}

	@Override
	public  boolean authenticate(EmployeeVO emp, String pass) throws IncompleteEmployeeException {
		if(emp!=null && pass!=null){
			return EmployeeDAO.getInstance().authenticate(new Employee(emp), pass);
		}else{
			throw new IncompleteEmployeeException("Faltan datos al empleado");
		}
	}
	
	private EmployeeVO[] toVOs(Employee[] emp){
		EmployeeVO[] temp = (emp!=null) ? new EmployeeVO[emp.length] : null;
		int i = 0;
		for(Employee e : emp){
			temp [ i ] = e.toVO();
			i++;
		}return temp;
	}

}
